package com.wonder.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.system.UserInfo;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

public class MapUtils {
    /**
     * 将实体类变成map对象
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            map.put(fieldName, value);
        }
        return map;
    }

//    public static Map<String, Object> objectToMap(Object obj) {
//        return Arrays.stream(obj.getClass().getDeclaredFields())
//                .peek(field -> field.setAccessible(true))
//                .collect(Collectors.toMap(Field::getName, field -> {
//                    try {
//                        return field.get(obj);
//                    } catch (IllegalAccessException e) {
//                        return null;
//                    }
//                }));
//    }

    public static List<Map<String, Object>> objectToList(List<Object> data) {

        List<Map<String, Object>> objectList = data.stream()
                .map(obj -> BeanUtil.beanToMap(obj))
                .collect(Collectors.toList());
     return objectList;
    }

}
